import { Vector2 } from "@/utils/Vector2"
import { CustomElement } from "@/js/components/CustomElement"
import { Line } from "@/js/components/Group"
import { LinkNode } from "../../LinkNode/src"

export class LinkNodeLine extends Line{
    bgColor = "#fff"

    startPointTarget?: LinkNode
    endPointTarget?: LinkNode

    startPoint = new Vector2()
    endPoint = new Vector2()

    setStartPoint(x: number,y: number){
        this.set(0, x, y)
    }

    setEndPoint(x: number,y: number){
        this.set(1, x, y)
    }

    /**
     * 
     * @param e 
     */
    drawTarget(e: ((HTMLElement | SVGElement ) & {[k:string]: any}) | CustomElement){ 
        if(e instanceof Element) e.appendChild(this.$el)
        else if(e instanceof CustomElement) e.appendChild(this)
    }

    del(){
        this.startPointTarget?.delLinkNodeLine(this)
        this.endPointTarget?.delLinkNodeLine(this)
        this.removeTo()
    }

    setTarget(target1: LinkNode, target2: LinkNode){
        if(target1.index === 0){
            this.startPointTarget = target2
            this.endPointTarget = target1
        }
        else{
            this.startPointTarget = target1
            this.endPointTarget = target2
        }
    }

    getLinkTargetUuid(index: number){
        return index === 1 ? {
            uuid: this.endPointTarget?.graphNode.uuid,
            name: this.endPointTarget?.name,
            index: 0,
            position: {
                x: this.pointEnd.x,
                y: this.pointEnd.y
            }
        } : {
            uuid: this.startPointTarget?.graphNode.uuid,
            name: this.startPointTarget?.name,
            index: 1,
            position: {
                x: this.pointStart.x,
                y: this.pointStart.y
            }
        }
    }
}
